﻿var IsAddNew = false;
var curTr;
$(document).ready(function () {
    $("#btnAddNew").click(function () {
        $("#divBarSearch, #divListItem").css("display", "none");
        $("#divAddNew").css("display", "");
        IsAddNew = true;
        $("#trAddNew").css("display", "none");
    });
    $("#btnEdit").click(function () {
        $("#divBarSearch, #divListItem").css("display", "");
        $("#divAddNew").css("display", "none");
        $("#divAdd").css("display", "none");
    });

    $("#btnSaveDetailEnrolling").click(function () {
        var iSchoolId = $("select[id$='ddlSchoolAdd'] option:selected").val();
        var iMajorId = $("select[id$='ddlMajorAdd'] option:selected").val();
        var iField1 = $("select[id$='ddlFieldAdd1'] option:selected").val();
        var iField2 = $("select[id$='ddlFieldAdd2'] option:selected").val();
        var iNumRegister = parseFloat($.trim($("input[id$='txtRegister']").val()));
        var iTarget = parseFloat($.trim($("input[id$='txtTarget']").val()));
        var iScore = parseFloat($.trim($("input[id$='txtScore']").val()));
        //var iCompetitor = $.trim($("input[id$='txtCompetitor']").val());
        var iCompetitor = parseFloat(iNumRegister / iTarget);
        
        var iYear = parseInt($.trim($("input[id$='txtYear']").val()));

        $.ajax({
            url: "DetailEnrolling.aspx/InsertDetailEnrolling",
            type: "POST",
            data: "{iSchoolId:" + iSchoolId + ",iMajorId:" + iMajorId + ",iField1:" + iField1 + ",iField2:" + iField2 + ",iNumRegister:" + iNumRegister + ",iTarget:" + iTarget + ",iScore:" + iScore + ",iYear:" + iYear + ",fNumCompetitor:" + iCompetitor + "}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                if (parseInt(data.d) === 1) {
                    $("span[id$='spResult']").text("Thêm chi tiết ngành mới thành công");
                } else {
                    $("span[id$='spResult']").text("Thêm chi tiết ngành mới thát bại. Vui lòng thử lại lần sau");
                }
            },
            error: function () {
                alert('error');
                $("span[id$='spResult']").text("Thêm chi tiết ngành trường mới thát bại. Vui lòng thử lại lần sau");
            }
        });

    });

    $("select[id$='ddlRegionAdd']").change(function () {
        var iRegionId = parseInt($(this).val());

    });

    $("select[id$='ddlSchoolAdd']").change(function () {
        var iSchoolId = parseInt($(this).val());
        var sName = '';
        $.ajax({
            url: "DetailEnrolling.aspx/GetMajor",
            type: "POST",
            data: "{iMajorId:0,sName:'" + sName + "',iSectionId:0,iSchoolId:" + iSchoolId + ",isActive:1,isDelete:0}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) { getMajorComp(data); },
            error: function () {
                onError();
            }
        });


    });

    $(".aEditDetail").click(function () {
        curTr = $(this).parent().parent();
        var iNumRegister = parseInt($("input[id$='txtRegister']", curTr).val());
        var iTargets = parseInt($("input[id$='txtTarget']", curTr).val());
        var iScore = parseFloat($("input[id$='txtScore']", curTr).val());
        var fCompetitor = parseFloat($("input[id$='txtCompetitor']", curTr).val());
        var iDetailEnrollingId = parseInt($("span[id$='lbDetailEnrollingId']", curTr).text());


        $.ajax({
            url: "DetailEnrolling.aspx/UpdateDetailRolling",
            type: "POST",
            data: "{iId:" + iDetailEnrollingId + ",iNumRegister:" + iNumRegister + ",iTargets:" + iTargets + ",iScore:" + iScore + ",fCompetitor:" + fCompetitor + "}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) { updateDetailComp(data, curTr); },
            error: function () {
                onError();
            }
        });


    });

});

function updateDetailComp(data) {
    if( parseInt(data.d) === 1) {
        $("img[id$='ImgStatus']", curTr).attr("src", "../Images/check.png").height(20);
    } else {
        $("img[id$='ImgStatus']", curTr).attr("src", "../Images/error.png");
    }
}

function getMajorComp(data) {
    var sRes = JSON.parse(data.d);
    var List = sRes.Lst;

  
    $("select[id$='ddlMajorAdd'] option").remove();
    if (List !== null) {
        var iLen = parseInt(List.length);
        if (parseInt(iLen) > 0) {
            for (var i = 0; i < iLen; i++) {
                $("select[id$='ddlMajorAdd']").append($('<option>').text(List[i].Name).val(List[i].Id));
            }
        }
    }
    $("select[id$='ddlMajorAdd']").prepend($('<option>').text("-- Chọn ngành --").val('0'));

}

function onError() {
    alert('Error');
}